﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using Web.Filter;
using Web.Models;
using System.Xml;
using fqlmoney.Common;
using fqlmoney.Model;
using fqlmoney.BLL;

namespace fqlmoney.Controllers
{
    
    public class OrderController : Controller
    {

        OrderBLL bll = new OrderBLL();

        /// <summary>
        /// 账单list
        /// </summary>
        /// <returns></returns>
        [CheckAdminLogin]
        public ActionResult List(int page = 1)
        {
            MoneyTypeBLL typebll = new MoneyTypeBLL();
            userinfo model_userinfo = BaseOperater.getAdmin(HttpContext);
            string extsql = " and user_order.u_id=" + model_userinfo.u_id;
            int totalPages = 0;
            int totalItems = 0;
            int pageCount = 10;

            ViewBag.list = bll.GetPage(extsql, page, pageCount, ref totalPages, ref totalItems);
            ViewBag.userinfo = model_userinfo;
            ViewBag.request_url = Request.Path.ToString();
            ViewBag.page = page;
            ViewBag.pagesize = pageCount;
            ViewBag.search_url = "";
            ViewBag.totalpage = totalPages;
            ViewBag.totalcount = totalItems;
            ViewBag.typelist = typebll.GetList(model_userinfo.u_id);
            return View();
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [CheckAdminLoginAjax]
        public JsonResult Add()
        {
            userinfo model_userinfo = BaseOperater.getAdmin(HttpContext);
            user_order model = new user_order();
            model.o_amount = Utils.StrToDecimal(Request.Form["o_amount"],0);
            model.o_note = Request.Form["o_note"];
            model.o_state = Utils.StrToInt(Request.Form["o_state"],2);
            model.o_time =Utils.StrToDateTime(Request.Form["o_time"],DateTime.Now);
            model.t_id = Utils.StrToInt(Request.Form["t_id"],0);
            model.u_id = model_userinfo.u_id;
            
            return Json(bll.Add(model));
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [CheckAdminLoginAjax]
        public JsonResult Update()
        {
            user_order model = new user_order();
            model.o_amount = Utils.StrToDecimal(Request.Form["o_amount"], 0);
            model.o_note = Request.Form["o_note"];
            model.o_state = Utils.StrToInt(Request.Form["o_state"], 2);
            model.o_time = Utils.StrToDateTime(Request.Form["o_time"], DateTime.Now);
            model.t_id = Utils.StrToInt(Request.Form["t_id"], 0);
            model.o_id = Utils.StrToInt(Request.Form["o_id"], 0);

            return Json(bll.Update(model));
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <returns></returns>
        [CheckAdminLoginAjax]
        public JsonResult Delete()
        {
            userinfo model_userinfo = BaseOperater.getAdmin(HttpContext);
            return Json(bll.Delete(Utils.StrToInt(Request.Form["id"],0), model_userinfo.u_id));
        }

    }
}